/**
 * 
 */
package com.hp.bon.sgw.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.hp.bon.sgw.bean.SysParamBean;
import com.hp.bon.sgw.bean.bossnm.BossnmParam;

/**
 * 信息点
 * 
 * @author sunhaiy
 * 
 */
public class BossnmService {

	private static final Logger logger = LoggerFactory.getLogger(BossnmService.class);

	private ConfigService configService;
	private DataStatusService dataStatusService;
	private String configFile;
	private String dataPath;
	private String srcDataPath;

	private int sysLoc;
	private int neTypeId;
	private String areaCode;
	private Map<Integer, BossnmParam> map = new HashMap<Integer, BossnmParam>();

	public void init() {
		logger.info("init bossnm service ...");
		areaCode = configService.getAreaCode();
		sysLoc = configService.getIntValue(SysParamBean.KEY_SYSTEM_DR_ID, 0);
		neTypeId = configService.getIntValue(SysParamBean.KEY_SYSTEM_NeTypeId,
				3013);

		File file = new File(dataPath);
		if (!file.exists()) {
			file.mkdir();
		}
		File configfile = new File(this.configFile);
		if (configfile.exists()) {
			try {
				map = this.loadBossnmParams(configfile);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		BossnmParam param = null;
		param = map.get(BossnmParam.BossnmKey_ProcessLog);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_ProcessLog);
			map.put(BossnmParam.BossnmKey_ProcessLog, param);
		}
		param = map.get(BossnmParam.BossnmKey_ReqMsg);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_ReqMsg);
			map.put(BossnmParam.BossnmKey_ReqMsg, param);
		}
		param = map.get(BossnmParam.BossnmKey_AskMsg);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_AskMsg);
			map.put(BossnmParam.BossnmKey_AskMsg, param);
		}
		param = map.get(BossnmParam.BossnmKey_Top100);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_Top100);
			map.put(BossnmParam.BossnmKey_Top100, param);
		}
		param = map.get(BossnmParam.BossnmKey_MsgStat);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_MsgStat);
			map.put(BossnmParam.BossnmKey_MsgStat, param);
		}
		param = map.get(BossnmParam.BossnmKey_SysEvent);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_SysEvent);
			map.put(BossnmParam.BossnmKey_SysEvent, param);
		}
		param = map.get(BossnmParam.BossnmKey_SysWarn);
		if (param == null) {
			param = new BossnmParam(this.sysLoc, this.neTypeId, this.areaCode,
					BossnmParam.BossnmKey_SysWarn);
			map.put(BossnmParam.BossnmKey_SysWarn, param);
		}
	}

	public void excute() {
		boolean doBossnm = false;
		// 进程日志
		while (map.get(BossnmParam.BossnmKey_ProcessLog) != null
				&& map.get(BossnmParam.BossnmKey_ProcessLog).getEndTime() < System
						.currentTimeMillis()) {
			doBossnm = true;
			if (logger.isInfoEnabled()) {
				logger.info("genarate bossnm ProcessLog ...");
			}
			dataStatusService.genarateBossnmProcessLog(dataPath,
					map.get(BossnmParam.BossnmKey_ProcessLog),
					map.get(BossnmParam.BossnmKey_SysEvent),
					map.get(BossnmParam.BossnmKey_SysWarn));
		}
		// 请求类消息传送情况日志信息点
		while (map.get(BossnmParam.BossnmKey_ReqMsg) != null
				&& map.get(BossnmParam.BossnmKey_ReqMsg).getEndTime() < System
						.currentTimeMillis()) {
			doBossnm = true;
			if (logger.isInfoEnabled()) {
				logger.info("genarate bossnm RequsetMsg ...");
			}
			dataStatusService.genarateBossnmRequsetMsg(dataPath,
					map.get(BossnmParam.BossnmKey_ReqMsg));
		}
		// 应答类消息传送情况日志信息点
		while (map.get(BossnmParam.BossnmKey_AskMsg) != null
				&& map.get(BossnmParam.BossnmKey_AskMsg).getEndTime() < System
						.currentTimeMillis()) {
			doBossnm = true;
			if (logger.isInfoEnabled()) {
				logger.info("genarate bossnm AskMsg ...");
			}
			dataStatusService.genarateBossnmAskMsg(dataPath,
					map.get(BossnmParam.BossnmKey_AskMsg));
		}
		// 关键字top100
		while (map.get(BossnmParam.BossnmKey_Top100) != null
				&& map.get(BossnmParam.BossnmKey_Top100).getEndTime() < System
						.currentTimeMillis()) {
			doBossnm = true;
			if (logger.isInfoEnabled()) {
				logger.info("genarate bossnm KeyTop100 ...");
			}
			dataStatusService.genarateKeyTop100(dataPath, srcDataPath,
					map.get(BossnmParam.BossnmKey_Top100));
		}
		// 消息统计
		while (map.get(BossnmParam.BossnmKey_MsgStat) != null
				&& map.get(BossnmParam.BossnmKey_MsgStat).getEndTime() < System
						.currentTimeMillis()) {
			doBossnm = true;
			if (logger.isInfoEnabled()) {
				logger.info("genarate bossnm MsgStat ...");
			}
			dataStatusService.genarateBossnmMsgStat(dataPath,
					map.get(BossnmParam.BossnmKey_MsgStat));
		}

		// 更新配置文件
		if (doBossnm) {
			saveBossnmParams();
		}
	}

	/**
	 * 加载配置
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	private Map<Integer, BossnmParam> loadBossnmParams(File file)
			throws Exception {
		LineNumberReader reader = null;
		Map<Integer, BossnmParam> map = new LinkedHashMap<Integer, BossnmParam>();
		try {
			reader = new LineNumberReader(
					new InputStreamReader(new BufferedInputStream(
							new FileInputStream(file)), "utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				BossnmParam paramBean = new Gson().fromJson(line,
						BossnmParam.class);
				map.put(paramBean.getBossnmKey(), paramBean);

			}
			return map;
		} finally {
			if (reader != null) {
				reader.close();
			}
		}
	}

	/**
	 * 保存配置
	 */
	private void saveBossnmParams() {
		if (logger.isInfoEnabled()) {
			logger.info("update bossnm config file ...");
		}
		FileWriter fw = null;
		try {
			fw = new FileWriter(this.configFile);
			for (BossnmParam p : map.values()) {
				fw.write(new Gson().toJson(p) + "\r\n");
			}
		} catch (IOException e) {
			logger.error("Save Bossnm Config File error. " + e);
		} finally {
			try {
				fw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 请求类消息传送情况日志
	 */
	public void requestMsgLog() {

	}

	/**
	 * 应答类消息传送情况日志
	 */
	public void answerMsgLog() {

	}

	@Autowired
	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}

	@Autowired
	public void setDataStatusService(DataStatusService dataStatusService) {
		this.dataStatusService = dataStatusService;
	}

	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}

	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

	public void setSrcDataPath(String srcDataPath) {
		this.srcDataPath = srcDataPath;
	}

}